#pragma once

namespace lyz
{
	template<class T>
	struct listNode
	{
		listNode<T>* _prev;
		listNode<T>* _next;
		T _data;

		listNode(const T& x = T())
			:_data(x)
			,_prev(nullptr)
			,_next(nullptr)
		{
		}
	};
	template<class T>
	struct __list_iterator
	{
		typedef listNode<T> Node;
		Node* _node;
		typedef __list_iterator<T> self;

		__list_iterator(Node* node)
			:_node(node)
		{

		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		self operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		self operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		T& operator*()
		{
			return _node->_data;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}

	};

	template<class T>
	class list
	{
		typedef listNode<int> Node;
	public:
		typedef __list_iterator<T> iterator;
		list()

		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			
		}
		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		void push_back(const T& x)
		{
			Node* newnode = new Node(x);

			Node* tail = _head->_prev;
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
		}



	private:
		Node* _head;
	};

	void test_list()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			//*it += 10;

			cout << *it << " ";
			++it;
		}
		/*for (auto e : lt)
		{
			cout << e << " ";
		}*/

	}
}
